package com.huangguolin.cloud.spring.util;

//import com.huangguolin.cloud.OBS.OBSOperate.operate;
import com.obs.services.IObsClient;
import com.obs.services.ObsClient;
import com.obs.services.model.*;

import java.util.List;

public class OBSUtil {

//    private static final String endpoint="obs.cn-north-4.myhuaweicloud.com";
//    private static final String ak = "MUJTRUF9BIQVBOYWWJSN";
//    private static final String sk = "x2NlOjlbYegTn05kaF3tjPKJZkxwNrjazsWINrpo";
//    private static final String bucketName = "java-group2";


    public static void main(String[] args) {




//        ObsClient obsClient = new ObsClient(ak,sk,endpoint);
//         ObjectListing list = obsClient.listObjects(bucketName);
//        List<ObsObject> listObjects = list.getObjects();
//        //文件浏览
//        for(ObsObject obsObject:listObjects){
//            System.out.println("obsKey:" + obsObject.getObjectKey());
//            System.out.println("obsSize:" +
//                    obsObject.getMetadata().getContentLength());
//        }
//
//
//        //下载文件
//        ObsObject obsObject = obsClient.getObject(bucketName, "OIP-C.jpg");
//        InputStream is = obsObject.getObjectContent();
//        byte[] block = new byte[1024];
//        Integer length = 0;
//        try( FileOutputStream fileOutputStream = new
//                FileOutputStream("D:\\logfile\\OIP-C.jpg");
//        ){
//            while ((length = is.read(block)) > 0 ){
//                fileOutputStream.write(block,0,length);
//                fileOutputStream.flush();
//            }
//        }catch (IOException e1){
//            e1.printStackTrace();
//        }

//        //文件上传
//        try ( FileInputStream inputStream = new
//                FileInputStream("D:\\logfile\\OIP-C.jpg");){
//            obsClient.putObject(bucketName,"OIP-C.jpg",inputStream);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

//        //文件的删除
//        String objKey = "OIP-C.jpg";
//        if(obsClient.doesObjectExist(bucketName,objKey)){
//            obsClient.deleteObject(bucketName,objKey);
//        }
//
//        //文件的分享
//        String obsKey = "OIP-C.jpg";
//        TemporarySignatureRequest request = new TemporarySignatureRequest();
//        request.setBucketName(bucketName);
//        request.setObjectKey(obsKey);
//        request.setRequestDate(new Date());
//        request.setExpires(60*60*10);
//        TemporarySignatureResponse signature =
//                obsClient.createTemporarySignature(request);
//        String url = signature.getSignedUrl();
//        System.out.println(url);
        String obskey = "application.properties";
        AccessControlList acl = new AccessControlList();

        IObsClient obsClient = null;
        String bucketName = "";
        AccessControlList obsObject = obsClient.getBucketAcl(bucketName);
        Owner owner = obsObject.getOwner();
        acl.setOwner(owner);
        acl.grantPermission(new CanonicalGrantee(owner.getId()), Permission.PERMISSION_FULL_CONTROL);
        acl.grantPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_READ);
        acl.grantPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_WRITE);
        acl.grantPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_READ_ACP);
        acl.grantPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_WRITE_ACP);
        obsClient.setObjectAcl(bucketName, obskey, acl);

        acl = obsClient.getObjectAcl(bucketName, obskey);
        for(GrantAndPermission grantAndPermission: acl.getGrantAndPermissions()){
            System.out.println(grantAndPermission.getPermission());
        }
    }
}